package com.wawayaya.db.bookreader;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.io.File;

public class StrokeDB {
    private static final String TAG = StrokeDB.class.getSimpleName();
    /// database version
    private static final int DATABASE_VERSION = 1;
    /// name of database
    public static final String DATABASE_NAME = "stroke.db";
    /// name of table
    public static final String TABLE_STROKE = "tb_stroke";

    public static final String COLUMN_CODE = "code";
    public static final String COLUMN_INDEX = "strokeindex";
    public static final String COLUMN_NAME = "strokename";
    public static final String COLUMN_PTX = "x";
    public static final String COLUMN_PTY = "y";
    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_ANGLE = "endAngle";
    public static final String COLUMN_OUTLINE = "outline";
    public static final String COLUMN_MIDLINE = "midline";
    /// sql statement to create table
    private static final String CREATE_SQL_TABLE_STROKE =
            "CREATE TABLE "+TABLE_STROKE+"("+
                    COLUMN_CODE+" text not null,"+
                    COLUMN_INDEX+" integer not null,"+
                    COLUMN_NAME+" text not null,"+
                    COLUMN_PTX+" integer not null,"+
                    COLUMN_PTY+" integer not null,"+
                    COLUMN_TYPE+" integer not null,"+
                    COLUMN_ANGLE+" integer not null,"+
                    COLUMN_OUTLINE+" blob,"+
                    COLUMN_MIDLINE+" blob,"+
                    "PRIMARY KEY("+COLUMN_CODE+", "+COLUMN_INDEX+"));";

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        public DatabaseHelper(Context context, String filepath)
        {
            super(context, filepath + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(CREATE_SQL_TABLE_STROKE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_STROKE);
            onCreate(db);
        }
    }

    public SQLiteDatabase mstrokeDB = null;

    public StrokeDB(Context context, String path) {
        DatabaseHelper dbHelper = new DatabaseHelper(context, path);
        if (dbHelper != null)
            mstrokeDB = dbHelper.getWritableDatabase();
    }

    public void closeDB() {
        if (mstrokeDB != null) {
            mstrokeDB.close();
            mstrokeDB = null;
        }
    }
}
